package com.hanson.controller;

import com.alibaba.fastjson.JSONObject;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.util.FileUploadUtil;
import com.hanson.entity.FileBean;
import com.hanson.entity.FileTypeBean;
import com.hanson.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/admin/file")
public class FileController {
    @Autowired
    private FileService fileService;
    @Value("${oss.local}")
    private String isLocal;

    /**
     * 公共图片组件
     *
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/image", method = RequestMethod.GET)
    public String image(ModelMap modelMap, String parentinputname) {
        modelMap.put("parentinputname", parentinputname);
        return "admin/public/image";
    }

    @RequestMapping(value = "/addType", method = RequestMethod.GET)
    public String addcate(ModelMap modelMap, String id) {
        if (StringUtils.isNotEmpty(id)) {
            modelMap.put("name", fileService.selectFileType(Integer.parseInt(id)).getName());
            modelMap.put("id", id);
        }

        return "admin/public/addType";
    }

    @RequestMapping(value = "/addType", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage addType(FileTypeBean fileTypeBean) {
        ResultMessage resultMessage = new ResultMessage();
        fileService.addFileType(fileTypeBean);
        resultMessage.setMsg("新增成功");
        return resultMessage;
    }

    @RequestMapping(value = "/updateType", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage updateType(FileTypeBean fileTypeBean) {
        ResultMessage resultMessage = new ResultMessage();
        fileService.modifyFileType(fileTypeBean);
        resultMessage.setMsg("修改成功");
        return resultMessage;
    }

    @RequestMapping(value = "/deleteType/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage deleteType(@PathVariable Integer id) {
        ResultMessage resultMessage = new ResultMessage();
        fileService.deleteFileType(id);
        resultMessage.setMsg("删除成功");
        return resultMessage;
    }

    @RequestMapping(value = "getImageCate", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage getImageCate(String search) {
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setData(fileService.getAllFileTypes(search));
        return resultMessage;
    }

    @RequestMapping(value = "getImageList/pid/{pid}/page/{page}/limit/{limit}", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage getImageList(@PathVariable int pid, @PathVariable int page, @PathVariable int limit) {
        ResultMessage resultMessage = new ResultMessage();
        List<FileBean> fileBeanList = fileService.getImageList(pid, page, limit);
        JSONObject jsonObject = new JSONObject();
        if (CollectionUtils.isEmpty(fileBeanList)) {
            jsonObject.put("count", 0);
        } else {
            jsonObject.put("count", fileService.getImageCount(pid));
        }
        jsonObject.put("list", fileBeanList);
        resultMessage.setData(jsonObject);
        return resultMessage;
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage importRule(HttpServletRequest request, Integer pid) throws Exception {
        ResultMessage resultMessage = new ResultMessage();
        JSONObject fileObject = FileUploadUtil.uploadOssFile(request, isLocal);
        fileService.addFile(fileObject.getString("src"), fileObject.getString("fileName"), fileObject.getInteger("fileSize"), pid);
        resultMessage.setData(fileObject);
        resultMessage.setMsg("上传成功");
        return resultMessage;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage delete(FileBean fileBean) {
        ResultMessage resultMessage = new ResultMessage();
        fileService.deleteImage(fileBean.getImageId());
        resultMessage.setMsg("删除成功");
        return resultMessage;
    }

}
